package org.creavi.engine.model;

import java.io.IOException;
import java.util.ArrayList;

import com.jme.util.export.InputCapsule;
import com.jme.util.export.JMEExporter;
import com.jme.util.export.JMEImporter;
import com.jme.util.export.OutputCapsule;
import com.jme.util.export.Savable;

public class ModelDefinition implements Savable {
	private String name;

	private ArrayList<MeshDefinition> meshes;

	private ArrayList<JoinPointDefinition> joinPoints;

	private int uses;

	private float frameMultiplier;

	public ModelDefinition() {

	}

	public ModelDefinition(String name) {
		super();
		this.name = name;
		meshes = new ArrayList<MeshDefinition>();
		joinPoints = new ArrayList<JoinPointDefinition>();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getUses() {
		return uses;
	}

	public void setUses(int uses) {
		this.uses = uses;
	}

	public ArrayList<MeshDefinition> getMeshes() {
		return meshes;
	}

	public ArrayList<JoinPointDefinition> getJoinPoints() {
		return joinPoints;
	}

	public synchronized void use() {
		uses++;

		for (MeshDefinition part : meshes) {
			part.use();
		}
	}

	public synchronized void release() {
		uses--;

		for (MeshDefinition part : meshes) {
			part.release();
		}
	}

	public Class<?> getClassTag() {
		return getClass();
	}

	@SuppressWarnings("unchecked")
	public void read(JMEImporter im) throws IOException {
		InputCapsule ic = im.getCapsule(this);
		name = ic.readString("name", null);
		uses = ic.readInt("uses", 0);
		meshes = ic.readSavableArrayList("meshes",
				new ArrayList<MeshDefinition>());
		joinPoints = ic.readSavableArrayList("join",
				new ArrayList<JoinPointDefinition>());
	}

	public void write(JMEExporter ex) throws IOException {
		OutputCapsule oc = ex.getCapsule(this);
		oc.write(name, "name", null);
		oc.write(0, "uses", 0);
		oc.writeSavableArrayList(meshes, "meshes", null);
		oc.writeSavableArrayList(joinPoints, "join", null);
	}

	public float getFrameMultiplier() {
		return frameMultiplier;
	}

	public void setFrameMultiplier(float frameMultiplier) {
		this.frameMultiplier = frameMultiplier;
	}
}
